package it.softphone.cmc.server.service.impl;

import it.softphone.cmc.event.service.EventManagerService;
import it.softphone.cmc.server.entity.AccountEntity;
import it.softphone.cmc.server.service.SystemManagerService;
import it.softphone.cmc.server.service.exception.CMCException;
import it.softphone.cmc.shared.entity.Account;
import it.softphone.commons.service.LogAware;
import it.softphone.commons.service.security.SecurityAccountService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;

/**
 * 
 * @author bsorrentino
 *
 */
@Service
public class GAEEventManagerServiceImpl implements EventManagerService, LogAware {

	@Autowired SystemManagerService system;
	@Autowired SecurityAccountService security;
	
	private String getChannelId( Account account ) {

		String channelId = new StringBuilder()
		.append('{')
		//.append("tenant:'").append(loggedAccount.getTenant()).append('\'')
		.append("uid:'").append(account.getUID()).append('\'')
		.append('}')
		.toString();

		logger.debug( String.format("ChannelId: [%s] ", channelId ));
		
		return channelId;
	}
	
	private String registerChannelIdToLoggedAccount() throws CMCException {

		//SecurityAccountService security = ApplicationConfig.getBean(SecurityAccountService.class);

		final Account loggedAccount = security.getPrincipal(AccountEntity.class);

		final String channelId = getChannelId( loggedAccount );

/*		
		AttributeUtils.putAttribute(loggedAccount, "channelId", channelId);

		EntityManager em = NucleusJPAHelper.getEntityManager(loggedAccount);
		
		System.out.printf( "\n\n===> NucleusJPAHelper.getEntityManager(loggedAccount)=%s\n\nt", em );
		
		system.execNewTransaction("save account", new EMTask<Void>() {

			@Override
			public Void exec(EntityManager em) throws Exception {
				em.merge(loggedAccount);
				return null;
			}
			
		});
*/		
	
/*		
		final Account updatableAccount = system.findAccountById(loggedAccount.getId());

		AttributeUtils.putAttribute(updatableAccount, "channelId", channelId);

		system.storeAccount(updatableAccount);
*/			
		return channelId;
	
	}
	
	/**
	 * @throws CMCException 
	 * 
	 */
	public String createToken() throws CMCException {
		
		String channelId = registerChannelIdToLoggedAccount();
		
		ChannelService channelService = ChannelServiceFactory.getChannelService(); 

		return channelService.createChannel( channelId ); 
	}

	/**
	 * 
	 */
	@Override
	public void sendEvent(String event, String toAccount) throws CMCException {
		
		final Account account = system.findAccountById(toAccount);
		
		if( account == null ) {
			throw new CMCException( String.format("account [%s] not found!", toAccount) );
		}
		
		ChannelService channelService = ChannelServiceFactory.getChannelService();
		channelService.sendMessage(new ChannelMessage( getChannelId(account), event));

	}


}
